package manager;
import Database.Connectmysql;
import User.Login;

import java.io.IOException;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class ManagerLogin extends JFrame{

    private JTextField name;
    private JPasswordField password;
    private JButton loginButton,returnButton;
    private JLabel usernameLabel,passwordLabel;

    public ManagerLogin(){
        setTitle("管理员登录");
        //getContentPane().setBackground(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(300, 100, 450, 300);
        setLayout(null);


        usernameLabel = new JLabel("用户名：");
        passwordLabel = new JLabel("密码：");
        name = new JTextField(30);
        password= new JPasswordField(30);
        loginButton = new JButton("登录");
        returnButton = new JButton("返回");
        usernameLabel.setBounds(100,45,50,40);
        passwordLabel.setBounds(100,95,50,40);
        name.setBounds(200,50,150,30);
        password.setBounds(200,100,150,30);
        loginButton.setBounds(175,200,100,25);
        returnButton.setBounds(175,230,100,25);

        add(usernameLabel);
        add(passwordLabel);
        add(name);
        add(password);
        add(loginButton);
        add(returnButton);

        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean bool =false;
                String username = name.getText();
                String passwords = new String(password.getPassword());
                Connectmysql cmysql = new Connectmysql(
                        "jdbc:mysql://localhost:3306/bookmanagement",
                        "root",
                        "root",
                        "com.mysql.cj.jdbc.Driver");
                try {
                    if(cmysql.open()){
                        ResultSet rst= cmysql.executeSql(
                                "select managerpassword from manager where managername=?",
                                new Object[]{username}
                        );
                        if(rst.next()){
                            String truepassword = rst.getString("managerpassword");
                            if(truepassword.equals(passwords)){
                                bool =true;
                            }
                        }
                        rst.close();
                    }
                } catch (ClassNotFoundException f) {
                    f.printStackTrace();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if(bool){
                    new ManagerHome(username);
                    setVisible(false);
                }
                else{
                    System.err.println("用户名或密码错误");
                }
            }
        });

        returnButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    new Login();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
                setVisible(false);
            }
        });

        setVisible(true);

    }

}
